package com.nbgroup.controller;


import cn.hutool.core.bean.BeanUtil;
import com.nbgroup.pojo.Clue;
import com.nbgroup.pojo.common.PageBean;
import com.nbgroup.pojo.common.PageResult;
import com.nbgroup.pojo.common.Result;
import com.nbgroup.pojo.dto.AssignmentOrGainClueDTO;
import com.nbgroup.pojo.dto.ClueDTO;
import com.nbgroup.pojo.dto.CluePageQueryDTO;
import com.nbgroup.pojo.dto.CluePoolPageQueryDTO;
import com.nbgroup.pojo.vo.ClueVO;
import com.nbgroup.service.IClueService;
import com.nbgroup.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-26
 */
@RestController
@RequestMapping("/clues/clue")
@Api("线索接口")
public class ClueController {

    @Autowired
    private IClueService clueService;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 线索分页查询
     */
    @GetMapping("/list")
    @ApiOperation("线索分页查询")
    public PageBean PageQuery(CluePageQueryDTO dto) { // 该注解用于接收嵌套参数
        return clueService.pageQuery(dto);
    }

    /**
     * 新增线索
     */
    @PostMapping
    @ApiOperation("新增线索")
    public Result add(@RequestBody ClueDTO dto) {
        Clue clue = BeanUtil.copyProperties(dto, Clue.class);
        clueService.save(clue);
        return Result.success();
    }

    /**
     * 查看线索
     */
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Long id) {
        Clue clue = clueService.getById(id);
        ClueVO clueVO = BeanUtil.copyProperties(clue, ClueVO.class);
        return Result.success(clueVO);
    }

    /**
     * 分配线索
     *
     * @return
     */
    @PutMapping("/assignment")
    @ApiOperation("分配线索")
    public Result assignClue(@RequestBody AssignmentOrGainClueDTO dto) {
        clueService.lambdaUpdate().in(Clue::getId, dto.getIds())
                .set(Clue::getOwnerId, dto.getUserId())
                .update();
        return Result.success();
    }


    /**
     * 上传文件
     */
    @PostMapping("/importData")
    @ApiOperation("上传文件")
    public Result uploadData(MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();
        String name = UUID.randomUUID().toString() + file.getOriginalFilename();
        String url = aliOssUtil.upload(bytes, name);
        return Result.success(url);
    }

    /**
     * 线索池
     */
    @GetMapping("/pool")
    @ApiOperation("线索池分页查询")
    public Result cluePoolPageQuery(@ModelAttribute CluePoolPageQueryDTO dto){
        PageResult result = clueService.cluePoolPageQuery(dto);
        return Result.success(result);
    }

    /**
     * 线索捞取
     */
    @PutMapping("/gian")
    public Result gainClue(@RequestBody AssignmentOrGainClueDTO dto){
        Boolean flag = clueService.gainClue(dto);
        if (flag){
            return Result.error("已达最大持有数,捞取失败");
        }
        return Result.success();
    }
}
